using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Data;
using System.Windows;
using System.Globalization;

namespace MyPublisher.Windows.Controls.Primitives
{
    [ValueConversion(typeof(decimal?), typeof(String))]
    public class Decimal2StringConverter : IValueConverter
    {
        public static Decimal2StringConverter Instance
        {
            get { return InstanceOf<Decimal2StringConverter>.GetInstance(); }
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value == DBNull.Value)
                return string.Empty;
            else
            {
                return ((decimal)value).ToString();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value.ToString() == string.Empty)
                return null;
            else
            {
                return decimal.Parse(value.ToString());
            }
        }

        #endregion
    }
}
